package com.ea.eadp.nexus.test.common;

import com.ea.eadp.nexus.test.common.ApiClients.NexusGatewayApi;
import com.ea.eadp.rest.commerce.generated.*;
import com.ea.fusion.commerce.securemtx.SecureMTXBalanceConsumeRequest;
import com.ea.fusion.commerce.securemtx.SecureMTXDurableFulfillmentRequest;

import java.util.HashMap;

/**
 * Created by YimingCao on 3/10/2015.
 */
public class SecureMTXTestHelper {
    public SecureMTXTestHelper() {
    }

    //v1
    public ConsoleFulfillmentResponseType beginConsume(ConsoleFulfillmentRequestType request, AccessToken token) throws Exception {
        String requestString = TestUtil.parseObjectXml(request, "consoleFulfillmentRequest");
        HashMap<String, String> context = new HashMap<String, String>();
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.console_begin_consume_post, context, token, requestString, true, null);
        ConsoleFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, ConsoleFulfillmentResponseType.class);
        return fulfillResponse;
    }

    public ConsoleFulfillmentResponseType consume(String transactionId, AccessToken token) throws Exception {
        return consume(transactionId, token, null);
    }

    public ConsoleFulfillmentResponseType consume(String transactionId, AccessToken token, String ps4ticket) throws Exception {
        HashMap<String, String> context = new HashMap<String, String>();
        context.put("transactionId", transactionId);
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.console_consume_post, context, token, ps4ticket == null ? null : "ps4_ticket=" + ps4ticket);
        ConsoleFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, ConsoleFulfillmentResponseType.class);
        return fulfillResponse;
    }

    public ConsoleFulfillmentResponseType fulfilcomplete(String transactionId, AccessToken token) throws Exception {
        HashMap<String, String> context = new HashMap<String, String>();
        context.put("transactionId", transactionId);
        String response = NexusGatewayApi.instance().put(NexusGatewayApiEnum.console_exfulfillcomplete_put, context, token, null, true, null);
        ConsoleFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, ConsoleFulfillmentResponseType.class);
        return fulfillResponse;
    }

    public void cancle() {

    }

    public ConsoleFulfillmentResponseTypeList get(String platform, String entitlementLabel, AccessToken token) throws Exception {
        HashMap<String, String> context = new HashMap<String, String>();
        context.put("platform", platform);
        context.put("entitlementLabel", entitlementLabel);
        String response = NexusGatewayApi.instance().get(NexusGatewayApiEnum.console_get, context, token, true, null);
        ConsoleFulfillmentResponseTypeList fulfillmentResponseTypeList = TestUtil.parseJson(response, ConsoleFulfillmentResponseTypeList.class, "consoleFulfillmentResponseList");
        return fulfillmentResponseTypeList;
    }

    public ConsoleFulfillmentRequestType constructConsoleFulfillmentRequestType(String platform, String ipAddress, String locale, String country, String entitlementLabel, String offerID) {
        ConsoleFulfillmentRequestType consoleFulfillmentRequestType = new ConsoleFulfillmentRequestType();
        consoleFulfillmentRequestType.setPlatform(platform);
        consoleFulfillmentRequestType.setIpAddress(ipAddress);
        consoleFulfillmentRequestType.setLocale(locale);
        consoleFulfillmentRequestType.setCountry(country);
        consoleFulfillmentRequestType.setEntitlementLabel(entitlementLabel);
        ConsoleFulfillmentRequestType.FirstPartyOffer firstPartyOffer = new ConsoleFulfillmentRequestType.FirstPartyOffer();
        firstPartyOffer.setOfferId(offerID);
        consoleFulfillmentRequestType.setFirstPartyOffer(firstPartyOffer);
        return consoleFulfillmentRequestType;
    }

    public ConsoleFulfillmentRequestType constructConsoleFulfillmentRequestType(String platform, String ipAddress, String locale, String country, String entitlementLabel, Integer quantity, Boolean hasExternalFulfillment) {
        ConsoleFulfillmentRequestType consoleFulfillmentRequestType = new ConsoleFulfillmentRequestType();
        consoleFulfillmentRequestType.setPlatform(platform);
        consoleFulfillmentRequestType.setIpAddress(ipAddress);
        consoleFulfillmentRequestType.setLocale(locale);
        consoleFulfillmentRequestType.setCountry(country);
        consoleFulfillmentRequestType.setEntitlementLabel(entitlementLabel);
        ConsoleFulfillmentRequestType.FirstPartyOffer firstPartyOffer = new ConsoleFulfillmentRequestType.FirstPartyOffer();
        firstPartyOffer.setQuantity(quantity);
        firstPartyOffer.setHasExternalFulfillment(hasExternalFulfillment);
        consoleFulfillmentRequestType.setFirstPartyOffer(firstPartyOffer);
        return consoleFulfillmentRequestType;
    }

    //v2
    public SecureMTXFulfillmentResponseType postSecureMTX(AccessToken token) throws Exception {
        SecureMTXFulfillmentRequestType fulfillmentRequestType = new SecureMTXFulfillmentRequestType();
        fulfillmentRequestType.setCountry("US");
        fulfillmentRequestType.setLocale("en_US");
        fulfillmentRequestType.setPlatform("PS4");
        fulfillmentRequestType.setEntitlementLabel("fifaPoints");
        SecureMTXFulfillmentRequestType.FirstPartyOffer firstPartyOffer = new SecureMTXFulfillmentRequestType.FirstPartyOffer();
        firstPartyOffer.setQuantity(1);
        firstPartyOffer.setHasExternalFulfillment(true);
        fulfillmentRequestType.setFirstPartyOffer(firstPartyOffer);

        String requestString = TestUtil.parseObjectXml(fulfillmentRequestType, "consoleFulfillmentRequest");
        HashMap<String, String> context = new HashMap<String, String>();
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.securemtx_post, context, token, requestString, true, null);
        SecureMTXFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, SecureMTXFulfillmentResponseType.class);
        return fulfillResponse;
    }

    public SecureMTXFulfillmentResponseType postSecureMTXactionConsume(String transactionId, AccessToken token, String ps4ticket) throws Exception
    {
        HashMap<String, String> context = new HashMap<String, String>();
        context.put("transactionId", transactionId);
        String queryString = null;
        queryString = "action=consume";
        if (ps4ticket != null) queryString += "&ps4_ticket=" + ps4ticket;
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.securemtx_action_post, context, token, queryString);
        SecureMTXFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, SecureMTXFulfillmentResponseType.class);
        return fulfillResponse;
    }

    public SecureMTXBalanceGetResponseType getSecureMTXbalance(AccessToken token, String ps4ticket) throws Exception
    {
        String queryString = "platform=PS4&entitlementLabels=PLAT01?service_label=6";
        if (ps4ticket!=null) queryString += "&ps4_ticket=" + ps4ticket;
        HashMap<String, String> context = new HashMap<String, String>();
        String response = NexusGatewayApi.instance().get(token, NexusGatewayApiEnum.securemtx_balance_get, context, queryString, true);
        SecureMTXBalanceGetResponseType fulfillResponse = TestUtil.parseJson(response, SecureMTXBalanceGetResponseType.class, "secureMTXBalanceGetResponse");
        return fulfillResponse;
    }

    public SecureMTXBalanceConsumeResponseType postSecureMTXbalance(AccessToken token, String ps4ticket) throws Exception
    {
        SecureMTXBalanceConsumeRequestType secureMTXBalanceConsumeRequestType = new SecureMTXBalanceConsumeRequestType();
        secureMTXBalanceConsumeRequestType.setQuantity(1);
        secureMTXBalanceConsumeRequestType.setEntitlementLabel("PLAT01?service_label=6");
        secureMTXBalanceConsumeRequestType.setPlatform("PS4");
        secureMTXBalanceConsumeRequestType.setPs4Ticket(ps4ticket);

        String requestString = TestUtil.parseObjectXml(secureMTXBalanceConsumeRequestType, "secureMTXBalanceConsumeRequest");

        HashMap<String, String> context = new HashMap<String, String>();
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.securemtx_balance_post, context, token, requestString, true);
        SecureMTXBalanceConsumeResponseType fulfillResponse = TestUtil.parseXml(response, SecureMTXBalanceConsumeResponseType.class);
        return fulfillResponse;
    }

    public SecureMTXDurableFulfillmentResponseType postSecureMTXdurable(AccessToken token, String ps4ticket) throws Exception
    {
        SecureMTXDurableFulfillmentRequestType secureMTXDurableFulfillmentRequestType = new SecureMTXDurableFulfillmentRequestType();
        secureMTXDurableFulfillmentRequestType.setPlatform("PS4");
        secureMTXDurableFulfillmentRequestType.setEntitlementLabel("fifa15");
        secureMTXDurableFulfillmentRequestType.setIpAddress("127.0.0.1");
        secureMTXDurableFulfillmentRequestType.setFirstPartyOfferId("PS4DurableTestOfferId_2");
        secureMTXDurableFulfillmentRequestType.setCountry("US");
        secureMTXDurableFulfillmentRequestType.setLocale("en_US");
        secureMTXDurableFulfillmentRequestType.setPs4Ticket(ps4ticket);

        String requestString = TestUtil.parseObjectXml(secureMTXDurableFulfillmentRequestType, "secureMTXDurableFulfillmentRequest");

        HashMap<String, String> context = new HashMap<String, String>();
        String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.securemtx_durable_post, context, token, requestString, true);
        SecureMTXDurableFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, SecureMTXDurableFulfillmentResponseType.class);
        return fulfillResponse;
    }

    /*
    SecureMTXFulfillmentRequestType request = new SecureMTXFulfillmentRequestType();
    request.setPlatform("PS4");
    request.setIpAddress("127.0.0.1");
    request.setLocale("en_US");
    request.setEntitlementLabel("fifaPoints");
    request.setCountry("US");
    SecureMTXFulfillmentRequestType.FirstPartyOffer firstPartyOffer = new SecureMTXFulfillmentRequestType.FirstPartyOffer();
    firstPartyOffer.setOfferId("PS4ConsumableTestOfferId");
    request.setFirstPartyOffer(firstPartyOffer);
    String requestString = TestUtil.parseObjectXml(request, "secureMTXFulfillmentRequest");
    HashMap<String, String> context = new HashMap<String, String>();
    String response = NexusGatewayApi.instance().post(NexusGatewayApiEnum.securemtx_post, context, token2, requestString, true, null);
    SecureMTXFulfillmentResponseType fulfillResponse = TestUtil.parseXml(response, SecureMTXFulfillmentResponseType.class);
*/
}
